package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemu;
import cn.stylefeng.guns.sys.modular.system.entity.ZxKemuKey;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxKemuKeyMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxKemuMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.ZxKemuKeyParam;
import cn.stylefeng.guns.sys.modular.system.model.result.ZxKemuKeyResult;
import cn.stylefeng.guns.sys.modular.system.service.ZxKemuKeyService;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-01-07
 */
@Service
public class ZxKemuKeyServiceImpl extends ServiceImpl<ZxKemuKeyMapper, ZxKemuKey> implements ZxKemuKeyService {
    @Resource
    private ZxKemuMapper zxKemuMapper;


    @Override
    public ResponseData add(ZxKemuKeyParam param) {
        ZxKemuKey entity = getEntity(param);

        //必须是二级科目
        ZxKemu zxKemu = zxKemuMapper.selectById(param.getBtyZxKemuId());
        if (!zxKemu.getLevel().equals("2")) {
            return new ErrorResponseData("只能选择二级科目");
        }

        //关键字不能重复
        QueryWrapper<ZxKemuKey> wrapper = new QueryWrapper<>();
        wrapper = wrapper.eq("keyword", entity.getKeyword().trim());
        wrapper = wrapper.eq("del_flag", 0);
        List<ZxKemuKey> zxKemuKeyResultList = this.baseMapper.selectList(wrapper);

        if (CollectionUtil.isNotEmpty(zxKemuKeyResultList)) {
            return new ErrorResponseData("关键字已经被使用，请使用其它关键字");
        }
        //去掉关键字收尾的空格
        entity.setKeyword(entity.getKeyword().trim());
        this.save(entity);
        return ResponseData.success();
    }

    @Override
    public void delete(ZxKemuKeyParam param) {
//        this.removeById(getKey(param));

        ZxKemuKey zxKemuKey = this.getById(getKey(param));
        //逻辑删除
        zxKemuKey.setDelFlag(1);
        zxKemuKey.setUpdateUser(LoginContextHolder.getContext().getUserId());
        zxKemuKey.setUpdateUserView(LoginContextHolder.getContext().getUser().getName());
        this.updateById(zxKemuKey);
    }

    @Override
    public void update(ZxKemuKeyParam param) {
        ZxKemuKey oldEntity = getOldEntity(param);
        ZxKemuKey newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);

        //去掉关键字收尾的空格
        newEntity.setKeyword(newEntity.getKeyword().trim());
        this.updateById(newEntity);
    }

    @Override
    public ZxKemuKeyResult findBySpec(ZxKemuKeyParam param) {
        return null;
    }

    @Override
    public List<ZxKemuKeyResult> findListBySpec(ZxKemuKeyParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ZxKemuKeyParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public ZxKemuKey getDetailById(Integer id) {
        ZxKemuKey zxKemuKey = this.baseMapper.getDetailById(id);
        return zxKemuKey;
    }

    @Override
    public List<ZxKemuKeyResult>  getAllKemuKeyList(ZxKemuKeyParam zxKemuKeyParam) {
        return this.baseMapper.customList(zxKemuKeyParam);
    }

    private Serializable getKey(ZxKemuKeyParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ZxKemuKey getOldEntity(ZxKemuKeyParam param) {
        return this.getById(getKey(param));
    }

    private ZxKemuKey getEntity(ZxKemuKeyParam param) {
        ZxKemuKey entity = new ZxKemuKey();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


    @Override
    public ZxKemuKeyResult findByKeywords(String keywords){
        ZxKemuKeyResult rv = null;
        ZxKemuKeyParam para = new ZxKemuKeyParam();
        List<ZxKemuKeyResult> keyList = this.baseMapper.customList(para);
        if(keyList!=null){
            for(ZxKemuKeyResult r:keyList){
                String keywords_tmp = r.getKeyword();
                if(keywords_tmp.indexOf(keywords)>-1 || keywords.indexOf(keywords_tmp)>-1){
                    rv = r;
                    break;
                }
            }
        }
        return rv;
    }

    @Override
    public void updateJgc(String id, String isJgc) {
        this.getBaseMapper().updateJgc(id,isJgc);
    }

}
